package com.lsht.web.controller.siwei;

import com.github.pagehelper.PageInfo;
import com.lsht.common.bo.IdListAcceptBo;
import com.lsht.common.core.controller.BaseController;
import com.lsht.common.core.domain.AjaxResult;
import com.lsht.common.core.domain.model.LoginUser;
import com.lsht.common.utils.ServletUtils;
import com.lsht.common.utils.poi.ExcelUtil;
import com.lsht.framework.web.service.SysLoginService;
import com.lsht.framework.web.service.TokenService;
import com.lsht.system.domain.SysLogininfor;
import com.lsht.system.domain.siwei.SiweiDevice;
import com.lsht.system.domain.siwei.SiweiPoint;
import com.lsht.system.service.siwei.SiweiDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * 设备管理Controller
 *
 * @author zyc
 * @date 2024-08-14
 */
@RestController
@RequestMapping("/siwei/device")
@Api(value = "SiweiDevice", description = "设备管理", tags = "设备管理")
public class SiweiDeviceController extends BaseController {
    @Autowired
    private SiweiDeviceService siweiDeviceService;


    @Autowired
    private SysLoginService sysLoginService;

    @Autowired
    private TokenService tokenService;

    /**
     * 新增设备管理
     */
    @PostMapping(ADD_FUNCTION_URL)
    @ApiOperation(value = ADD_FUNCTION, httpMethod = "POST", response = AjaxResult.class, tags = "设备管理")
//    @PreAuthorize("@ss.hasPermi('siwei:device:add')")
    public AjaxResult genAdd(SiweiDevice siweiDevice, MultipartFile file) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        //获取部门ID
        Long deptId = loginUser.getUser().getDeptId();
        siweiDevice.setDeptId(deptId);
        return toAjax(siweiDeviceService.genAdd(siweiDevice, file));
    }

    /**
     * 删除设备管理
     */
    @PostMapping(DELETE_BY_ID_FUNCTION_URL)
    @ApiOperation(value = DELETE_BY_ID_FUNCTION, httpMethod = "POST", response = AjaxResult.class, tags = "设备管理")
    @PreAuthorize("@ss.hasPermi('siwei:device:remove')")
    public AjaxResult genDeleteById(@RequestBody IdListAcceptBo idListAcceptBo) {
        List<String> idList = idListAcceptBo.getIdList();
        if (CollectionUtils.isNotEmpty(idList)) {
            return toAjax(siweiDeviceService.genDeleteById(idList));
        }
        return AjaxResult.error();
    }

    /**
     * 修改设备管理
     */
    @PostMapping(UPDATE_BY_ID_FUNCTION_URL)
    @ApiOperation(value = UPDATE_BY_ID_FUNCTION, httpMethod = "POST", response = AjaxResult.class, tags = "设备管理")
    @PreAuthorize("@ss.hasPermi('siwei:device:edit')")
    public AjaxResult genUpdateById(@RequestBody SiweiDevice siweiDevice) {
        return toAjax(siweiDeviceService.genUpdateById(siweiDevice));
    }

    /**
     * 查询设备管理列表
     */
    @PostMapping(QUERY_LIST_FUNCTION_URL)
    @ApiOperation(value = QUERY_LIST_FUNCTION, httpMethod = "POST", response = SiweiDevice.class, tags = "设备管理")
//    @PreAuthorize("@ss.hasPermi('siwei:device:list')")
    public AjaxResult genQueryList(@RequestBody SiweiDevice siweiDevice) {
        startPage();
        List<SiweiDevice> list = siweiDeviceService.genQueryList(siweiDevice);
        return AjaxResult.success(new PageInfo<>(list));
    }

    /**
     * 查询设备管理不分页列表
     */
    @PostMapping("notPageDeviceList")
    @ApiOperation(value = "查询设备管理不分页列表", httpMethod = "POST", response = SiweiDevice.class, tags = "设备管理")
    //@PreAuthorize("@ss.hasPermi('siwei:device:list')")
    public AjaxResult notPageDeviceList(@RequestBody SiweiDevice siweiDevice) {
        List<SiweiDevice> list = siweiDeviceService.genQueryList(siweiDevice);
        return AjaxResult.success(list);
    }

    /**
     * 根据id查询设备详情信息
     */
    @PostMapping(QUERY_BY_ID_FUNCTION_URL)
    @ApiOperation(value = QUERY_BY_ID_FUNCTION, httpMethod = "POST", response = SiweiDevice.class, tags = "设备管理")
    //@PreAuthorize("@ss.hasPermi('siwei:device:query')")
    public AjaxResult genQueryById(@RequestBody SiweiDevice siweiDevice) {
        SiweiDevice device = siweiDeviceService.genQueryById(siweiDevice);
        return AjaxResult.success(device);
    }

    /**
     * 大屏查询设备数量及昨日新增数
     */
    @PostMapping("deviceStatistics")
    @ApiOperation(value = "大屏查询设备数量及昨日新增数", httpMethod = "POST", response = SiweiPoint.class, tags = "设备管理")
    public AjaxResult pointStatistics(@RequestBody SiweiDevice siweiDevice) {
        HashMap map = siweiDeviceService.deviceStatistics(siweiDevice);
        return AjaxResult.success(map);
    }

    /**
     * 大屏查询系统访问量
     */
    @PostMapping("systemStatistics")
    @ApiOperation(value = "大屏查询系统访问量", httpMethod = "POST", response = SiweiPoint.class, tags = "设备管理")
    public AjaxResult systemStatistics(@RequestBody SysLogininfor sysLogininfor) {
        HashMap map = siweiDeviceService.systemStatistics(sysLogininfor);
        return AjaxResult.success(map);
    }

    /**
     * 设备管理导入
     *
     * @param file
     * @param
     * @return
     * @throws Exception
     */
    @PostMapping("/importDevice")
    @ApiOperation(value = "设备管理导入", httpMethod = "POST", response = SiweiPoint.class, tags = "设备管理")
    public AjaxResult importDevice(MultipartFile file) throws Exception {
        ExcelUtil<SiweiDevice> util = new ExcelUtil<SiweiDevice>(SiweiDevice.class);
        List<SiweiDevice> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = siweiDeviceService.importDevice(userList, operName);
        return success(message);
    }

    /**
     * 设备管理导出
     *
     * @param response
     * @param
     */
    @PostMapping("/exportDevice")
    @ApiOperation(value = "设备管理导出", httpMethod = "POST", response = SiweiPoint.class, tags = "设备管理")
    public void exportDevice(HttpServletResponse response, SiweiDevice siweiDevice) {
        List<SiweiDevice> list = siweiDeviceService.genQueryList(siweiDevice);
        ExcelUtil<SiweiDevice> util = new ExcelUtil<SiweiDevice>(SiweiDevice.class);
        util.exportExcel(response, list, "点位管理数据");
    }

    /**
     * 设备管理模板导出
     *
     * @param response
     */
    @GetMapping("/importTemplate")
    @ApiOperation(value = "设备管理模板导出", httpMethod = "GET", response = SiweiPoint.class, tags = "设备管理")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<SiweiDevice> util = new ExcelUtil<SiweiDevice>(SiweiDevice.class);
        util.importTemplateExcel(response, "设备管理模板");
    }


}
